package com.ruoyi.project.business.domain.rules.daily;

import com.ruoyi.project.business.domain.entity.DailyScheduleEmp;
import com.ruoyi.project.business.domain.entity.Emp;
import com.ruoyi.project.business.domain.model.JudgeResult;
import com.ruoyi.project.business.domain.model.Solution;
import com.ruoyi.project.business.domain.model.daily.Post;
import com.ruoyi.project.business.domain.model.daily.TimePeriodMirror;
import com.ruoyi.project.business.domain.rules.ForManagerRule;
import com.ruoyi.project.business.domain.rules.Rule;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 技能是否匹配
 */
@Data
public class EmpSkillsAssign extends ForManagerRule {

    @Override
    public JudgeResult judge(Solution solution, Emp emp) {
        DailyScheduleEmp dailyScheduleEmp = (DailyScheduleEmp) emp;
        List<Post> empPosts = dailyScheduleEmp.getEmpPosts();
        TimePeriodMirror tryAssignTimePeriod = dailyScheduleEmp.getTryAssignTimePeriod();

        Long tmpPostId = tryAssignTimePeriod.getTmpPostId(); // 其实应该是技能  id

        boolean bingo = skillBingo(empPosts, tmpPostId);
        if(bingo) {
            // 如果未重复就接受
            return accept();
        } else {
            JudgeResult res = null;
            if (getBreakIfReject()) {
                res = stopTask();
            } else {
                res = acceptAndReturn();
            }
            res.setTips("emp EmpSkillsAssign 未匹配 " + tmpPostId);
            return res;
        }
    }

    private static boolean skillBingo(List<Post> empPosts, Long skillId) {
        if(CollectionUtils.isNotEmpty(empPosts)) {
            for (Post post : empPosts) {
                if(skillId.equals(post.getId())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public void setConflicts() {
        setType(Rule.RULE_TYPE_MANAGER);
        this.conflicts = new ArrayList<>();
    }
}
